package com.xtx.json;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * A JsonArray is an ordered sequence of values.
 * 
 * @author JSON.org
 * @author Chenin
 * @version 2007-1-17
 */
@SuppressWarnings("unchecked")
public class JsonArray implements Serializable, JsonEntity, Collection {
    private static final long serialVersionUID = 58617914720934632L;

    /**
     * The arrayList where the JsonArray's properties are kept.
     */
    private Collection array;

    /**
     * Construct an empty JsonArray.
     */
    public JsonArray() {
        this.array = new ArrayList();
    }

    public JsonArray(Collection array) {
        this.array = array;
    }

    /**
     * Construct a JsonArray from a JsonTokener.
     * 
     * @param tokener A JsonTokener object containing the source string
     * @throws JsonException If there is a syntax error in the source string
     */
    public JsonArray(JsonTokener tokener) throws JsonException {
        this();
        if (tokener.nextClean() != '[') {
            throw new JsonException("A JSON Array text must start with '['");
        }
        if (tokener.nextClean() == ']') {
            return;
        }
        tokener.back();
        while (true) {
            if (tokener.nextClean() == ',') {
                tokener.back();
                this.add(null);
            } else {
                tokener.back();
                this.add(tokener.nextValue());
            }
            switch (tokener.nextClean()) {
                case ';':
                case ',':
                    switch (tokener.nextClean()) {
                        case ']':
                            return;
                        case 0:
                            throw new JsonException("Expected a ',' or ']'");
                        default:
                            break;
                    }
                    tokener.back();
                    break;
                case ']':
                    return;
                default:
                    throw new JsonException("Expected a ',' or ']'");
            }
        }
    }

    public String toJsonString() {
        StringBuffer strb = new StringBuffer();
        strb.append("[");
        Iterator iter = this.array.iterator();
        int i = 0;
        while (iter.hasNext()) {
            if (i++ > 0) {
                strb.append(",");
            }
            strb.append(JsonPrinter.getString(iter.next()));
        }
        strb.append("]");
        return strb.toString();
    }

    public String toString() {
        return this.toJsonString();
    }

    public boolean add(Object e) {
        return this.array.add(e);
    }

    public boolean addAll(Collection c) {
        return this.array.addAll(c);
    }

    public void clear() {
        this.array.clear();
    }

    public boolean contains(Object o) {
        return this.array.contains(o);
    }

    public boolean containsAll(Collection c) {
        return this.array.containsAll(c);
    }

    public boolean isEmpty() {
        return this.array.isEmpty();
    }

    public Iterator iterator() {
        return this.array.iterator();
    }

    public boolean remove(Object o) {
        return this.array.remove(o);
    }

    public boolean removeAll(Collection c) {
        return this.array.removeAll(c);
    }

    public boolean retainAll(Collection c) {
        return this.array.retainAll(c);
    }

    public int size() {
        return this.array.size();
    }

    public Object[] toArray() {
        return this.array.toArray();
    }

    public Object[] toArray(Object[] a) {
        return this.array.toArray(a);
    }
}
